import requests
from lxml import etree

# url = "https://www.xbiquge.la/10/10489/4535761.html"


def get_response(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/81.0.4044.138 Safari/537.36 '
    }
    response = requests.get(url, headers)
    response.encoding = response.apparent_encoding
    response_text = response.text
    print("---------------------------------------响应的response内容---------------------------------------")
    print(response_text)
    print("---------------------------------------响应的response内容---------------------------------------")
    return response_text


def get_info_from_response(url):
    response_text = get_response(url)
    # 利用xpath得到自己想要的信息
    # 注意:网页上的xpath不一定是response_text中的xpath路径,路径可能会被改变

    # 对文本进行解析
    tree = etree.HTML(response_text)

    # 解析得到小说标题
    title = tree.xpath('//div[@class="bookname"]/h1/text()')[0]
    # 小说内容
    content_list = tree.xpath('//*[@id="content"]//text()')
    content_str = ''.join(content_list)

    # 打印已经得到的信息
    print("title:" + title)
    print("content:" + content_str)
    save(title, content_str)


def save(title, content):
    pass


if __name__ == '__main__':
    novel_id = input('输入书名ID：格式：/15/15409/')
    url = f'https://www.xbiquge.la{novel_id}'
    get_info_from_response(url)
